package scripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;
import utils.Log4Test;

/**
 * Created by skopeikin on 03.11.2014.
 */

public class AddSubscriptionsToNutrabulk {
    public static WebDriver driver;
    //Login section
    private static final By EMAIL_ADDRESS_LOCATOR = By.xpath("//input[@name='login[username]']");
    private static final By PASSWORD_LOCATOR = By.xpath("//input[@name='login[password]']");
    private static final By LOGIN_BUTTON_LOCATOR = By.xpath("//input[@type='submit']");
    //Admin Panel section
    private static final By CLOSE_POP_UP = By.xpath("/html/body/div[1]/div[4]/div[1]/a");
    private static final By SUBSCRIPTION_CATEGORY_LOCATOR = By.xpath("//*[@id='nav']/li[10]");
    private static final By MANAGE_SUBSCRIPTIONS_SUBCATEGORY_LOCATOR = By.xpath("//*[@id='nav']/li[10]/ul/li[2]/a");
    private static final By CATALOG_CATEGORY_LOCATOR = By.xpath("//*[@id=\"nav\"]/li[3]");
    private static final By MANAGE_PRODUCTS_SUBCATEGORY_LOCATOR = By.xpath("//*[@id=\"nav\"]/li[3]/ul/li[1]/a");
    //Product management
    private static final By SKU_INPUT_LOCATOR = By.xpath("//*[@id=\"productGrid_product_filter_sku\"]");
    private static final By SEARCH_BUTTON_LOCATOR = By.xpath("//*[@id=\"productGrid\"]/table/tbody/tr/td[2]/button[2]");
    private static final By FIRST_ELEMENT_IN_PRODUCTS_GRID = By.xpath("//*[@id=\"productGrid_table\"]/tbody/tr/td[3]");
    //Product details
    private static final By PRICES_TAB_LOCATOR = By.xpath("//*[@id=\"product_info_tabs_group_8\"]");
    private static final By SPECIAL_PRICE_LOCATOR = By.xpath("//*[@id=\"special_price\"]");
    //Subscriptions management
    private static final By ADD_NEW_BUTTON = By.xpath("//*[@id=\"page:main-container\"]/div[2]/table/tbody/tr/td[2]/button");
    private static final By SELECT_GRID_200_QUANTITY_LOCATOR = By.xpath("//*[@id='productGrid']/table/tbody/tr/td[1]/select/option[5]");
    private static final By GRID_SORTING_ORDER_LOCATOR = By.cssSelector("#productGrid_table > thead > tr.headings > th:nth-child(1) > span > a");
    private static final By PRICE_OF_FIRST_ELEMENT_IN_GRID = By.xpath("//*[@id=\"productGrid_table\"]/tbody/tr[1]/td[4]");
    private static final By SKU_OF_FIRST_ELEMENT_IN_GRID = By.xpath("//*[@id=\"productGrid_table\"]/tbody/tr[1]/td[3]");
    //Subscription
    private static final By START_SUBSCRIPTION_MOMENT_OF_PURCHASE_LOCATOR = By.xpath("//*[@id='start_date_code']/option[2]");
    private static final By SUBSCRIPTION_TYPES_TAB_LOCATOR = By.cssSelector("#subscription_tabs_subscription_types");
    private static final String SUBSCRIPTION_DATA_PRICE_PER_ITERATION_INPUT_LOCATOR = "//*[@id='subscription_tabs_subscription_types_content']/div/div[%d]/div/table/tbody/tr[2]/td[2]/input";
    String SUBSCRIPTION_DATA_SORTING_ORDER_INPUT_LOCATOR = "//*[@id=\"subscription_tabs_subscription_types_content\"]/div/div[%d]/div/table/tbody/tr[5]/td[2]/input";
    String SUBSCRIPTION_DATA_ADD_NEW_BUTTON_LOCATOR = "//*[@id=\"subscription_tabs_subscription_types_content\"]/div/div[%d]/div/table/tbody/tr[6]/td[2]/button[2]";
    private static final By SAVE_BUTTON_LOCATOR = By.xpath("//button[@title='Save']");


    @Test
    public void AddSubscriptions()
    {   driver = new FirefoxDriver();
        driver.manage().window().maximize();
        WebDriverWait wait = new WebDriverWait(driver,30);
        driver.get("http://nutrabulk.magentolive.nl/admin");
        //write email
        wait.until(ExpectedConditions.visibilityOfElementLocated(EMAIL_ADDRESS_LOCATOR));
        driver.findElement(EMAIL_ADDRESS_LOCATOR).clear();
        driver.findElement(EMAIL_ADDRESS_LOCATOR).sendKeys("admin");
        //write password
        driver.findElement(PASSWORD_LOCATOR).clear();
        driver.findElement(PASSWORD_LOCATOR).sendKeys("admin123");
        //click login
        driver.findElement(LOGIN_BUTTON_LOCATOR).click();
        //move to subscription category in navi menu
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(CLOSE_POP_UP)));
        driver.findElement(CLOSE_POP_UP).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(SUBSCRIPTION_CATEGORY_LOCATOR));
        Actions builder = new Actions(driver);
        builder.moveToElement(driver.findElement(SUBSCRIPTION_CATEGORY_LOCATOR));
        builder.perform();
        //choose subscription management subcategory
        wait.until(ExpectedConditions.visibilityOfElementLocated(MANAGE_SUBSCRIPTIONS_SUBCATEGORY_LOCATOR));
        driver.findElement(MANAGE_SUBSCRIPTIONS_SUBCATEGORY_LOCATOR).click();
        //click add new subscription
        for (int i = 0; i <149; i++) {
            wait.until(ExpectedConditions.visibilityOf(driver.findElement(ADD_NEW_BUTTON)));
            driver.findElement(ADD_NEW_BUTTON).click();
            //set grid quantity and grid sorting quantity
            //wait.until(ExpectedConditions.visibilityOf(driver.findElement(SELECT_GRID_200_QUANTITY_LOCATOR)));
            //driver.findElement(SELECT_GRID_200_QUANTITY_LOCATOR).click();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
            ;
            driver.findElement(SKU_INPUT_LOCATOR).clear();
            driver.findElement(SEARCH_BUTTON_LOCATOR).click();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
            ;
            String sku = driver.findElement(SKU_OF_FIRST_ELEMENT_IN_GRID).getText();
            Log4Test.info(sku);
            //go to manage products
            builder.moveToElement(driver.findElement(CATALOG_CATEGORY_LOCATOR));
            builder.perform();
            driver.findElement(MANAGE_PRODUCTS_SUBCATEGORY_LOCATOR).click();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            ;
            wait.until(ExpectedConditions.visibilityOf(driver.findElement(SKU_INPUT_LOCATOR)));
            driver.findElement(SKU_INPUT_LOCATOR).sendKeys(sku);
            driver.findElement(SEARCH_BUTTON_LOCATOR).click();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
            ;
            driver.findElement(FIRST_ELEMENT_IN_PRODUCTS_GRID).click();
            wait.until(ExpectedConditions.visibilityOf(driver.findElement(PRICES_TAB_LOCATOR)));
            driver.findElement(PRICES_TAB_LOCATOR).click();
            wait.until(ExpectedConditions.visibilityOf(driver.findElement(SPECIAL_PRICE_LOCATOR)));
            String specialPrice = driver.findElement(SPECIAL_PRICE_LOCATOR).getAttribute("value");
            Log4Test.info("Price = " + specialPrice);
            builder.moveToElement(driver.findElement(SUBSCRIPTION_CATEGORY_LOCATOR));
            builder.perform();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            ;
            driver.findElement(MANAGE_SUBSCRIPTIONS_SUBCATEGORY_LOCATOR).click();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            ;
            wait.until(ExpectedConditions.visibilityOf(driver.findElement(ADD_NEW_BUTTON)));
            driver.findElement(ADD_NEW_BUTTON).click();
            //wait.until(ExpectedConditions.elementToBeClickable(GRID_SORTING_ORDER_LOCATOR));
            //driver.findElement(GRID_SORTING_ORDER_LOCATOR).click();
            //select and edit product from grid
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
            ;
            driver.findElement(FIRST_ELEMENT_IN_PRODUCTS_GRID).click();
            //set moment of purchase
            wait.until(ExpectedConditions.visibilityOf(driver.findElement(START_SUBSCRIPTION_MOMENT_OF_PURCHASE_LOCATOR)));
            driver.findElement(START_SUBSCRIPTION_MOMENT_OF_PURCHASE_LOCATOR).click();
            //subscription types tab
            driver.findElement(SUBSCRIPTION_TYPES_TAB_LOCATOR).click();
            for (int j = 1; j < 9; j++) {
                wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath(String.format(SUBSCRIPTION_DATA_PRICE_PER_ITERATION_INPUT_LOCATOR, j)))));
                driver.findElement(By.xpath(String.format(SUBSCRIPTION_DATA_PRICE_PER_ITERATION_INPUT_LOCATOR, j))).clear();
                driver.findElement(By.xpath(String.format(SUBSCRIPTION_DATA_PRICE_PER_ITERATION_INPUT_LOCATOR, j))).sendKeys(specialPrice);
                driver.findElement(By.xpath(String.format(SUBSCRIPTION_DATA_SORTING_ORDER_INPUT_LOCATOR, j))).click();
                driver.findElement(By.xpath(String.format(SUBSCRIPTION_DATA_SORTING_ORDER_INPUT_LOCATOR, j))).sendKeys(String.valueOf(j - 1));
                driver.findElement(By.xpath(String.format(SUBSCRIPTION_DATA_ADD_NEW_BUTTON_LOCATOR, j))).click();
            }
            driver.findElement(SAVE_BUTTON_LOCATOR).click();
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
            }
            ;
        }

    }
    @AfterSuite(alwaysRun = true)
    public void shutInv()
    {
        if(driver != null)
            driver.quit();
    }
}
